﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NPlot.Windows;
using SleepMonitor.App.Business;

namespace SleepMonitor.App.UI
{
	partial class MainForm
	{
		private interface IInteraction
		{
			PlotSurface2D Control { get; }
		}


		private class HorizontalDrag : NPlot.Windows.PlotSurface2D.Interactions.HorizontalDrag, IInteraction
		{
			private readonly MainForm ownerForm;
			private PlotSurface2D control;

			public HorizontalDrag(MainForm form)
			{
				ownerForm = form;
			}


			public PlotSurface2D Control
			{
				get { return control; }
			}

			public override bool DoMouseMove(MouseEventArgs e, Control ctr, KeyEventArgs lastKeyEventArgs)
			{
				if (ownerForm.moveButton.Checked)
				{
					this.control = (PlotSurface2D)ctr;
					return base.DoMouseMove(e, ctr, lastKeyEventArgs);
				}
				return false;
			}
		}

		private class Selection : NPlot.Windows.PlotSurface2D.Interactions.HorizontalRangeSelection, IInteraction
		{
			private readonly MainForm ownerForm;
			private PlotSurface2D control;
			private bool selecting = false;

			public Selection(MainForm form)
			{
				ownerForm = form;
			}
            
			public PlotSurface2D Control
			{
				get { return control; }
			}

			public override bool DoMouseDown(MouseEventArgs e, Control ctr)
			{
				this.control = (PlotSurface2D)ctr;

				if (ownerForm.markButton.Checked)
				{
					if (!selecting)
					{
						selecting = true;

						try
						{
							foreach (var surface in ownerForm.surfaces)
							{
								var evt = new MouseEventArgs(e.Button, e.Clicks, e.X, surface.Parent.PointToScreen(surface.Location).Y, e.Delta);
								base.DoMouseDown(evt, surface);
							}

						}
						finally
						{
							selecting = false;
						}						
					}
					return base.DoMouseDown(e, ctr);
				}
				return false;
			}

			public override bool DoMouseMove(MouseEventArgs e, Control ctr, KeyEventArgs lastKeyEventArgs)
			{
				this.control = (PlotSurface2D)ctr;

				if (ownerForm.markButton.Checked)
				{
					if (!selecting)
					{
						selecting = true;

						try
						{
							if (e.Button == System.Windows.Forms.MouseButtons.Left)
							{
								foreach (var surface in ownerForm.surfaces)
								{
									var evt = new MouseEventArgs(e.Button, e.Clicks, e.X, surface.Parent.PointToScreen(surface.Location).Y, e.Delta);
									base.DoMouseMove(evt, surface, lastKeyEventArgs);
								}
							}
						}
						finally
						{
							selecting = false;
						}
					}
					return base.DoMouseMove(e, ctr, lastKeyEventArgs);
				}
				return false;
			}

			public override bool DoMouseUp(MouseEventArgs e, Control ctr)
			{
				this.control = (PlotSurface2D)ctr;

				if (ownerForm.markButton.Checked)
				{
					if (!selecting)
					{
						selecting = true;

						try
						{
							foreach (var surface in ownerForm.surfaces)
							{
								surface.DoMouseUp(e, surface);
							}

						}
						finally
						{
							selecting = false;
						}
					}
					return true;
				}
				return false;
			}
		}

		private class VerticalDrag : NPlot.Windows.PlotSurface2D.Interactions.VerticalDrag, IInteraction
		{
			private readonly MainForm ownerForm;
			private PlotSurface2D control;

			public VerticalDrag(MainForm form)
			{
				ownerForm = form;
			}


			public PlotSurface2D Control
			{
				get { return control; }
			}

			public override bool DoMouseMove(MouseEventArgs e, Control ctr, KeyEventArgs lastKeyEventArgs)
			{
				if (ownerForm.moveButton.Checked)
				{
					this.control = (PlotSurface2D)ctr;
					return base.DoMouseMove(e, ctr, lastKeyEventArgs);
				}
				return false;
			}
		}

	}


}
